package com.example.pattern.draw

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.pattern.data.PictureViewModel
import com.example.pattern.databinding.FragmentDraw2Binding
import com.example.pattern.db.Picture
import com.example.pattern.draw.color.ColorAdapter
import com.example.pattern.draw.color.ColorModel
import com.example.pattern.utils.ColorUtils
import com.example.pattern.utils.FileUtils
import com.example.pattern.utils.TimeUtils
import com.example.pattern.utils.convertViewToBitmap
import com.example.pattern.utils.scaleBitmap
import kotlinx.coroutines.launch

class DrawFragment : Fragment() {
    private lateinit var binding:FragmentDraw2Binding
    private val mViewModel:DrawViewModel by viewModels()
    private val mAdapter = ColorAdapter()
    private val args:DrawFragmentArgs by navArgs()
    private val mPictureViewModel:PictureViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentDraw2Binding.inflate(inflater,container,false)
        return binding.root
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        binding.drawView.apply {
            viewModel =  mViewModel
            lifeCycleOwner = viewLifecycleOwner
            args.currentPicture?.let {
                lifecycleScope.launch {
                    backgroundBitmap = FileUtils().loadBitmapFromPath(it.url)
                }
            }
        }
        args.currentPicture?.let {
            binding.editTextText.setText(it.name)
        }
        binding.toolContainer.viewModel = mViewModel
        binding.progressView.mDrawViewModel = mViewModel
        binding.colorRecyclerView.apply {
            layoutManager = LinearLayoutManager(context,RecyclerView.HORIZONTAL,false)
            adapter = mAdapter
        }
        mAdapter.mDrawViewModel = mViewModel
        mViewModel.mColorModels.observe(viewLifecycleOwner){
            mAdapter.setData(it)
        }
        binding.backBtn.setOnClickListener {
            if(args.currentPicture == null && !binding.drawView.isEmpty()){
                lifecycleScope.launch {
                    val bitmap = convertViewToBitmap(binding.drawView)
                    val time = TimeUtils.time()
                    val name = binding.editTextText.text.trim().toString()
                    val thumbnail = scaleBitmap(bitmap,0.5f)
                    val url = FileUtils().saveBitmap(bitmap)
                    mPictureViewModel.savePicture(Picture(0,name,time,url,thumbnail))
                }
            }
            args.currentPicture?.let {
                var shouldUpdate = false
                if(!binding.drawView.isEmpty()){
                    lifecycleScope.launch {
                        val bitmap = convertViewToBitmap(binding.drawView)
                        val thumbnail = scaleBitmap(bitmap,0.5f)
                        val path = FileUtils().saveBitmap(bitmap)
                        FileUtils().deleteFile(it.url)
                        it.url = path
                        it.thumbnail = thumbnail
                        shouldUpdate = true
                    }
                }
                if(it.name != binding.editTextText.text.toString()){
                    it.name = binding.editTextText.text.toString()
                    shouldUpdate = true
                }
                if(shouldUpdate){
                    mPictureViewModel.updatePicture(it)
                }
            }
            findNavController().navigateUp()
        }
    }
}